package com.li.o2oSystem.web.wechat;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.li.o2oSystem.util.weixin.SignUtil;

@Controller
@RequestMapping("wechat")
public class WechatController {
	private static Logger log = LoggerFactory.getLogger(WechatController.class);
	
	@RequestMapping(method = {RequestMethod.GET})
	public void doGet(HttpServletRequest request,HttpServletResponse response){
		log.debug("weixin get...");
		//微信加密签名，signature结合开发者填写的token参数，和请求中的timstamp参数，nonce参数
		String signature = request.getParameter("signature");
		//时间戳
		String timestamp = request.getParameter("timestamp");
		//随机数
		String nonce = request.getParameter("nonce");
		//随机字符串
		String echostr = request.getParameter("echostr");
		
		PrintWriter out = null;
		try{
			out = response.getWriter();
			if(SignUtil.checkSignature(signature, timestamp, nonce)){
				log.debug("weixin get success...");
				out.print(echostr);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(out != null){
				out.close();
			}
		}
	}
}
